package cn.itcast.controller;

import cn.itcast.pojo.*;
import cn.itcast.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @GetMapping("/list")
    public Result findAllClazz() {
        List<Clazz> clazzs = clazzService.findAllClazz();
        return  clazzs != null && clazzs.size() > 0 ? Result.success(clazzs) : Result.error("查询失败");
    }
    @GetMapping
    public Result findByColClazz(
            String name,
            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end,
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "10")Integer pageSize
    ){
        PageBean<Clazz> clazzs = clazzService.findByColClazz(name, begin, end, page, pageSize);
        return clazzs == null || clazzs.getRows().size() == 0? Result.error("查询失败") : Result.success("查询成功", clazzs);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean result = clazzService.deleteById(id);
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        boolean result = clazzService.addClazz(clazz);
        return result ? Result.success("添加成功") : Result.error("添加失败");
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        ClazzById clazzById = clazzService.findById(id);
        return clazzById == null ? Result.error("查询失败") : Result.success("查询成功", clazzById);
    }

    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        boolean result = clazzService.updateClazz(clazz);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }
    //studentCountData

}
